package com.yuanzong.wuliu.service.admin.business.customer;

import cn.hutool.core.collection.CollUtil;
import com.yuanzong.wuliu.common.exception.BusinessException;
import com.yuanzong.wuliu.common.utils.id.SnowFlakeUtils;
import com.yuanzong.wuliu.common.vo.ApiResult;
import com.yuanzong.wuliu.common.vo.ApiResultPage;
import com.yuanzong.wuliu.controller.admin.business.customer.param.*;
import com.yuanzong.wuliu.controller.intercdeptor.AdminHandlerInterceptorAdapter;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomer;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerDaijiaxian;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerDaijiaxianDetails;
import com.yuanzong.wuliu.model.business.customer.bo.BusinessCustomerDaijiaxianPageByMapperBo;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianDetailsListQuery;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianListQuery;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianPageQuery;
import com.yuanzong.wuliu.model.system.user.SystemUser;
import com.yuanzong.wuliu.service.business.customer.factory.BusinessCustomerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class AdminBusinessCustomerDaijiaxianService {

    public ApiResult page(AdminBusinessCustomerDaijiaxianPageParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerDaijiaxianPageQuery businessCustomerDaijiaxianPageQuery = new BusinessCustomerDaijiaxianPageQuery();
        BeanUtils.copyProperties(param, businessCustomerDaijiaxianPageQuery);
        ApiResultPage<BusinessCustomerDaijiaxianPageByMapperBo> apiResultPage = BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().pageByMapper(businessCustomerDaijiaxianPageQuery);
        return ApiResult.success(apiResultPage);
    }

    public ApiResult save(AdminBusinessCustomerDaijiaxianSaveParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomer businessCustomer = BusinessCustomerFactory.getBusinessCustomerService().queryByPhone(param.getCustomerPhone());
        if (businessCustomer == null) {
            throw new BusinessException("客户不存在");
        }
        BusinessCustomerDaijiaxianListQuery businessCustomerDaijiaxianListQuery = new BusinessCustomerDaijiaxianListQuery();
        businessCustomerDaijiaxianListQuery.setCustomerId(businessCustomer.getId());
        List<BusinessCustomerDaijiaxian> businessCustomerDaijiaxians = BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().querySubject(businessCustomerDaijiaxianListQuery);
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        if (CollUtil.isNotEmpty(param.getDetailses())) {
            BusinessCustomerFactory.getBusinessCustomerDaijiaxianDetailsService().removeByIds(param.getRemoveDetailsIds());
        }
        BusinessCustomerDaijiaxian businessCustomerDaijiaxian = null;
        if (CollUtil.isNotEmpty(businessCustomerDaijiaxians)) {
            businessCustomerDaijiaxian = businessCustomerDaijiaxians.get(0);
            businessCustomerDaijiaxian.setName(param.getName());
            businessCustomerDaijiaxian.setOrderNo(param.getOrderNo());
            businessCustomerDaijiaxian.setEndDate(LocalDate.parse(param.getEndDate()));
            BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().updateById(businessCustomerDaijiaxian);
        } else {
            businessCustomerDaijiaxian = new BusinessCustomerDaijiaxian();
            businessCustomerDaijiaxian.setId(SnowFlakeUtils.getId());
            businessCustomerDaijiaxian.setCustomerId(businessCustomer.getId());
            businessCustomerDaijiaxian.setName(param.getName());
            businessCustomerDaijiaxian.setOrderNo(param.getOrderNo());
            businessCustomerDaijiaxian.setEndDate(LocalDate.parse(param.getEndDate()));
            BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().save(businessCustomerDaijiaxian);
        }
        for (AdminBusinessCustomerDaijiaxianSaveDetailsParam detailsParam : param.getDetailses()) {
            BusinessCustomerDaijiaxianDetails businessCustomerDaijiaxianDetails = new BusinessCustomerDaijiaxianDetails();
            BeanUtils.copyProperties(detailsParam, businessCustomerDaijiaxianDetails);
            if (detailsParam.getId() == null) {
                businessCustomerDaijiaxianDetails.setId(SnowFlakeUtils.getId());
            }
            businessCustomerDaijiaxianDetails.setDaijiaxianId(businessCustomerDaijiaxian.getId());
            BusinessCustomerFactory.getBusinessCustomerDaijiaxianDetailsService().saveOrUpdate(businessCustomerDaijiaxianDetails);
        }
        return ApiResult.success();
    }

    public ApiResult info(AdminBusinessCustomerDaijiaxianInfoParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerDaijiaxian businessCustomerDaijiaxian = BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().getById(param.getId());
        BusinessCustomerDaijiaxianDetailsListQuery businessCustomerDaijiaxianDetailsListQuery = new BusinessCustomerDaijiaxianDetailsListQuery();
        businessCustomerDaijiaxianDetailsListQuery.setDaijiaxianId(param.getId());
        List<BusinessCustomerDaijiaxianDetails> daijiaxianDetailses = BusinessCustomerFactory.getBusinessCustomerDaijiaxianDetailsService().querySubject(businessCustomerDaijiaxianDetailsListQuery);
        businessCustomerDaijiaxian.setDetailses(daijiaxianDetailses);
        BusinessCustomer bussinessCustomer = BusinessCustomerFactory.getBusinessCustomerService().getById(businessCustomerDaijiaxian.getCustomerId());
        businessCustomerDaijiaxian.setCustomerPhone(bussinessCustomer.getPhone());
        return ApiResult.success(businessCustomerDaijiaxian);
    }

    public ApiResult remove(AdminBusinessCustomerDaijiaxianRemoveParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().removeById(param.getId());
        return ApiResult.success();
    }
}
